import { query,add,remove,edit ,editConfig,queryLogs,queryOneUsergroup } from '@/services/invoicings_manage';
import moment from 'moment';
export default {
  namespace: 'invoicings_manage',
  state: {
    pageLoaded:false,
    tableY:300,
    data:[],
    scrollData:{x:0,y:0},
    selectedRowKeys: [0],
    searchInputType:'order_number',
    searchInputText:'',
    searchParams:{
      start_date:moment(new Date().getFullYear()+'-'+(parseInt(new  Date().getMonth())+1)+'-'+'01' , "YYYY-MM-DD").format("YYYY-MM-DD"),
      end_date:moment(new Date()).format('YYYY-MM-DD'),
      date_type:"generation_date",
      order_number:"",
      invoice_code:'',
      invoice_number:'',
      buyer_name:'',
      clerk:'',
      invoicing_type:'',
      invoicing_statuses:'',
      modes:'',
      source:'',
      mobile:'',
      page: 1,
      per_page: 50,
    },
    meta: {total: 0, per_page: 50,current_page:1},
    summary: {total_amount: 0, total_count: 0},
    loading: true,
    editRecord:{},
    detail:{
      pageLoaded:false,
      isDetail:false,
    }

  },
  effects: {
    *changeIsDetail({ payload, callback }, { call, put ,select}) {
      console.log('changeIsDetail')
      const {detail} = yield select(state => state.invoicings_manage)
      yield put({
        type: 'changeDetail',
        payload:{
          ...detail,
          isDetail:!detail.isDetail,
          detailId:payload.id,
        }
      });
    },
    *changeOneProperty({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveOneProperty',
        payload:payload
      });
      setTimeout(function() {
        if(callback) callback()
      },0)

    },
    *reset({ payload, callback }, { call, put }) {
      console.log('reset=============')
      yield put({
        type: 'resetData',
        payload:payload
      });
      if(callback) callback()
    },
    *changeTableY({ payload, callback }, { call, put }) {
      const tableY=document.querySelector('.table-pagination').getBoundingClientRect().top -
        document.querySelector('.react-grid-Container').getBoundingClientRect().top -10;
      yield put({
        type: 'saveTableY',
        payload: tableY
      });
    },
    *changeSearchParams({ payload, callback }, { call, put }) {
      console.log('changeSearchParams')
      yield put({
        type: 'saveSearchParams',
        payload: payload
      });
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *selectRow({payload, callback}, {call, put,select}) {
      const {selectedRowKeys} = yield select(state => state.invoicings_manage)
      if (selectedRowKeys.indexOf(payload.id) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(payload.id), 1);
      } else {
        selectedRowKeys.push(payload.id);
      }
      yield put({
        type: 'saveSelectRow',
        payload: selectedRowKeys,
      });
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const state = yield select(state => state.invoicings_manage)//获取state中的数据
      payload=payload||state.searchParams;
      console.log('payload',payload)
      const response = yield call(query, {...payload,invoicing_statuses:payload.invoicing_statuses?[payload.invoicing_statuses]:'',
        modes:payload.modes?[payload.modes]:''});
      console.log(response)
      if(response.data.status_code===200){
        yield put({
          type: 'save',
          payload:  response.data
        });
        yield put({
          type: 'saveSearchParams',
          payload: payload
        });

        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }


    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);

      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }else{
      }
    },
    *edit({ payload, callback }, { call, put }) {
      console.log('edit')
      const response = yield call(edit, payload);
      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }
    },
    *changeLoadedFalse({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: false,
      });
      if (callback) callback();
    },
    *editConfig({ payload, callback }, { call, put }) {
      console.log('edit')
      const response = yield call(editConfig, payload);
      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }
    },
    *changeSelectRow({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload,
      });
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      if(response.status===200){
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
        meta:action.payload.meta,
        summary:action.payload.summary
      };
    },
    saveTableY(state, action) {
      return {
        ...state,
        tableY: action.payload,
      };
    },
    saveOneRecord(state, action) {
      return {
        ...state,
        editRecord:action.payload
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    saveSearchParams(state, action) {
      return {
        ...state,
        searchParams: {
          // ...state.searchParams,
          ...action.payload
        },
      };
    },
    saveOneProperty(state, action) {
      return {
        ...state,
        ...action.payload
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload,
        tableY:state.tableY,
        data:[],
        scrollData:{x:0,y:0},
        selectedRowKeys: [],
        searchInputType:'order_number',
        searchInputText:'',
        searchParams:{
          start_date:moment(new Date().getFullYear()+'-'+(parseInt(new  Date().getMonth())+1)+'-'+'01' , "YYYY-MM-DD").format("YYYY-MM-DD"),
          end_date:moment(new Date()).format('YYYY-MM-DD'),
          order_number:"",
          date_type:"generation_date",
          invoice_code:'',
          invoice_number:'',
          buyer_name:'',
          clerk:'',
          invoicing_type:'',
          invoicing_statuses:'',
          modes:'',
          source:'',
          mobile:'',
          page: 1,
          per_page: 50,
        },
        meta: {total: 0, per_page: 0,current_page:1},
        loading: true,
        editRecord:{},
        summary: {total_amount: 0, total_count: 0},
        detail:{
          pageLoaded:false,
          isDetail:false,
        }
      };
    },
    saveSelectRow(state, action) {
      return {
        ...state,
        selectedRowKeys: action.payload,
      };
    },
  },

};
